package dao;

import domain.User;
import utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class UserDaoImpl implements UserDao {
    //登录
    @Override
    public User Login(String username, String password) {
        Connection conn=null;
        PreparedStatement sta=null;
        ResultSet rs=null;
        User user=null;
        try {
            //获取连接
            conn= JdbcUtils.getConnection();
            //编写sql语句
            String sql= "select * from t_user where username=? and password=?";
            //预编译sql语句
            sta=conn.prepareStatement(sql);
            //设置值
            sta.setString(1,username);
            sta.setString(2,password);
            rs = sta.executeQuery();
            if(rs.next()){
                //封装数据
              // user =new User(rs.getInt("id"),rs.getString("username"),rs.getString("password"),rs.getString("nickname"));
               user=new User();
               user.setId(rs.getInt("id"));
               user.setUsername(rs.getString("username"));
               user.setPassword(rs.getString("password"));
               user.setNickname(rs.getString("nickname"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn,sta,rs);
        }
        return  user;
    }
}
